package com.spring.boot.demo.controller;

import com.spring.boot.demo.common.exception.BizException;
import com.spring.boot.demo.common.resp.Result;
import com.spring.boot.demo.pojo.User;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ZhuYouBin
 * @version 1.0.0
 * @Date: 2022/10/29 21:44
 * @Description
 */
@RestController
@RequestMapping("/api")
public class TestController {

    @GetMapping("/success")
    public Result<List<User>> success() {
        List<User> list = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            User user = new User();
            user.setUsername("text-name00" + i);
            user.setPassword("pass-00" + i);
            user.setEmail("test@qq.com");
            list.add(user);
        }
        return Result.<List<User>>success().data(list);
    }

    @GetMapping("/error")
    public Result<Void> failure() {
        int i = 10;
        // 模拟异常情况
        int ans = i / 0;
        return Result.success();
    }

    @GetMapping("/error02")
    public Result<Void> failure02() {
        int i = 10;
        // 模拟异常情况
        try {
            int ans = i / 0;
        } catch (Exception e) {
            throw new BizException("抛出自定义异常");
        }
        return Result.success();
    }

    @PostMapping("/error03")
    public Result<User> param(@RequestBody @Valid User user) {
        // 测试参数校验异常, 必须使用【@Valid】注解生效
        return Result.<User>success().data(user);
    }

}
